package net.elive.server;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.HashMap;

import net.elive.tools.Contants;
import net.elive.utils.ErrorMessage;
import net.elive.utils.LogUtils;

import org.apache.commons.logging.Log;

public class Config {
	Log log = LogUtils.getLog(Config.class);
	/**
	 * 版本
	 */
	public static short version = 0;
	private static Server server;
	public static DatagramSocket ds;
	/**
	 * 服务器是否启动
	 */
	public static boolean run = true;
	public static HashMap<Integer, Node> onlineNode = new HashMap<Integer, Node>(
			1000); // 在线节点数量
	public static HashMap<Integer, Node> strengthNode = new HashMap<Integer, Node>(
			1000); // 强节点数量
	public static HashMap<Integer, Group> resourceGroup = new HashMap<Integer, Group>(
			1000); // 资源数量
	public static ArrayList<Integer> strengthList =new ArrayList<Integer>();//强节点列表
	/**
	 * 消息接收队列
	 */
	public static ArrayList<DatagramPacket> messageQueue = new ArrayList<DatagramPacket>(); // 数据包接收队列
	public static MessageReceiver messageReceiver; // 消息接收线程
	public static MessageProcessor messageProcessor; // 消息接收进程
	/** 节点管理**/
	public static NodeManage nodeManage = new NodeManage();
	public static CleanTaskManage cleanTaskManage; // 清理节点任务管理线程
	
	public static Thread checkLanIpServerFirst; // 检测内网IP服服务1号端口线程
	public static Thread checkLanIpServerSecond; // 检测内网IP服服务2号端口线程
	public static Thread monitorThread; 		// 监听线程检测节点 资源状态

	public Config(Server server) {
		Config.server = server;
		try {
			ds = new DatagramSocket(Contants.C_PORT);
			messageReceiver = new MessageReceiver(); // 初始化消息接收线程
			log.info(ErrorMessage.INIT_SERVER_MESSAGE_RECEIVER_SUCCESS);
			messageProcessor = new MessageProcessor(); // 初始化消息接收进程
			log.info(ErrorMessage.INIT_SERVER_MESSAGEPROCESSOR_SUCCESS);
		} catch (SocketException e) {
			log.error(ErrorMessage.INIT_SERVER_MESSAGE_RECEIVER_ERROR);
			log.error(ErrorMessage.INIT_SERVER_MESSAGEPROCESSOR_ERROR);
			e.printStackTrace();
		}
		cleanTaskManage = new CleanTaskManage(); // 初始化清理节点任务管理线程
		/**
		 * 初始化内网ip检测服务 默认端口95和8080端口
		 */
		checkLanIpServer();
		/**
		 * 加载监听资源状态线程
		 */
		loadMonitorThread();
	}

	/**
	 * 初始化内网ip检测服务 默认端口95 和8080端口
	 */
	public void checkLanIpServer() {
		checkLanIpServerFirst = new Thread() {
			public void run() {
				try {
					ServerSocket ss = new ServerSocket(
							Contants.CHECK_LANIP_SERVERPORT_FIRST);
					while (true) {
						ss.accept();
					}
				} catch (IOException e) {
					log.error(">>>>>>>>"+Contants.CHECK_LANIP_SERVERPORT_FIRST+"端口已被占用");
				}
			}
		};
		checkLanIpServerFirst.start();
		checkLanIpServerSecond = new Thread() {
			public void run() {
				try {
					ServerSocket ss = new ServerSocket(
							Contants.CHECK_LANIP_SERVERPORT_SECOND);
					while (true) {
						ss.accept();
					}
				} catch (IOException e) {
					e.printStackTrace();
					log.error(">>>>>>>>"+Contants.CHECK_LANIP_SERVERPORT_SECOND+"端口已被占用");
				}
			}
		};
		checkLanIpServerSecond.start();
	}

	/**
	 * 加载监听资源状态线程
	 */
	public void loadMonitorThread(){
		monitorThread =new Thread(){
			public void run(){
				while(true){
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						log.error(ErrorMessage.SLEEP_THREAD_ERROR);
						e.printStackTrace();
					}
					log.info("当前在线节点数量>>>>>:"+Config.onlineNode.size());
					log.info("强节点数量>>>>>>>>>>>:"+Config.strengthNode.size()+"/"+Config.strengthList.size());
					log.info("资源总数>>>>>>>>>>>>>:"+Config.resourceGroup);
				}
			}
		};
		monitorThread.start();		//启动节点状态监听线程
	}
}
